package ClientController;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import ClientUI.ClientGUI;

public class ClientFileHelper implements Runnable{
	int  filePort;
	ClientGUI	cgui;
	File		file;
	Socket	 clientSocket;
	
	public ClientFileHelper(int port, ClientGUI	ui, File f) {
		this.filePort = port;
		this.cgui = ui;
		this.file = f;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			ServerSocket	ss = new ServerSocket(filePort);
			clientSocket = ss.accept();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(filePort == cgui.getClient().getClientData().getFileUploadPort()) {
			doUpload();
		}
		else if(filePort == cgui.getClient().getClientData().getFileDownloadPort()) {
			doDownload();
		}
	}
	
	private void doDownload() {
		OutputStream os = null ;
		try {
			os = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("file not exists !");
			e.printStackTrace();
		}
		DataOutputStream  fos = new DataOutputStream(os);
		DataInputStream fis=null;
		try {
			fis = new DataInputStream(clientSocket.getInputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		byte[]   buffer = new byte[1024];
		int len = 0;
		
		do {
			try {
				len = fis.read(buffer, 0, 1024);
				if(len != -1) {
					fos.write(buffer, 0, len);
					fos.flush();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while(len != -1);
		cgui.mainFrame.realMySpacePanel.setStatus(file.getName()+"download completed !");
		try {
			fis.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void doUpload() {
		InputStream fis = null ;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("file not exists !");
			e.printStackTrace();
		}
		DataInputStream   fin= new DataInputStream(fis);
		DataOutputStream fou=null;
		try {
			fou = new DataOutputStream(clientSocket.getOutputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		byte[]   buffer = new byte[1024];
		int len = 0;
		
		do {
			try {
				len = fin.read(buffer, 0, 1024);
				if(len != -1) {
					fou.write(buffer, 0, len);
					fou.flush();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while(len != -1);
		
		try {
			fin.close();
			fou.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
